<template>
  <div class="sql-snippets">
    <el-dialog
      v-model="dialogVisible"
      title="SQL代码段"
      width="800px"
      :close-on-click-modal="false"
    >
      <div class="snippets-container">
        <div class="snippets-header">
          <el-input
            v-model="searchText"
            placeholder="搜索代码段"
            clearable
            style="width: 200px"
          />
          <el-button type="primary" @click="addSnippet">添加代码段</el-button>
        </div>
        <div class="snippets-content">
          <el-table :data="filteredSnippets" style="width: 100%">
            <el-table-column prop="name" label="名称" width="150" />
            <el-table-column prop="description" label="描述" />
            <el-table-column label="操作" width="120">
              <template #default="scope">
                <el-button type="primary" link @click="useSnippet(scope.row)">
                  使用
                </el-button>
                <el-button type="danger" link @click="deleteSnippet(scope.row)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-dialog>

    <el-dialog
      v-model="editDialogVisible"
      :title="currentSnippet.id ? '编辑代码段' : '添加代码段'"
      width="600px"
      :close-on-click-modal="false"
    >
      <el-form :model="currentSnippet" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="currentSnippet.name" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="currentSnippet.description" type="textarea" />
        </el-form-item>
        <el-form-item label="代码">
          <scCodeEditor v-model="currentSnippet.code" mode="sql" style="width: 100%;height: 200px" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveSnippet">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import scCodeEditor from '@/components/scCodeEditor/index.vue'

const dialogVisible = ref(false)
const editDialogVisible = ref(false)
const searchText = ref('')
const snippets = ref([])
const currentSnippet = ref({
  id: null,
  name: '',
  description: '',
  code: ''
})

const filteredSnippets = computed(() => {
  if (!searchText.value) return snippets.value
  return snippets.value.filter(item => 
    item.name.toLowerCase().includes(searchText.value.toLowerCase()) ||
    item.description.toLowerCase().includes(searchText.value.toLowerCase())
  )
})

const loadSnippets = () => {
  // 从本地存储加载代码段
  const savedSnippets = localStorage.getItem('sqlSnippets')
  if (savedSnippets) {
    snippets.value = JSON.parse(savedSnippets)
  }
}

const saveSnippets = () => {
  // 保存代码段到本地存储
  localStorage.setItem('sqlSnippets', JSON.stringify(snippets.value))
}

const addSnippet = () => {
  currentSnippet.value = {
    id: null,
    name: '',
    description: '',
    code: ''
  }
  editDialogVisible.value = true
}

const editSnippet = (snippet) => {
  currentSnippet.value = { ...snippet }
  editDialogVisible.value = true
}

const saveSnippet = () => {
  if (!currentSnippet.value.id) {
    // 新增
    currentSnippet.value.id = Date.now()
    snippets.value.push({ ...currentSnippet.value })
  } else {
    // 更新
    const index = snippets.value.findIndex(item => item.id === currentSnippet.value.id)
    if (index !== -1) {
      snippets.value[index] = { ...currentSnippet.value }
    }
  }
  saveSnippets()
  editDialogVisible.value = false
}

const deleteSnippet = (snippet) => {
  const index = snippets.value.findIndex(item => item.id === snippet.id)
  if (index !== -1) {
    snippets.value.splice(index, 1)
    saveSnippets()
  }
}

const useSnippet = (snippet) => {
  emit('use', snippet.code)
  dialogVisible.value = false
}

const emit = defineEmits(['use'])

defineExpose({
  show: () => {
    loadSnippets()
    dialogVisible.value = true
  }
})
</script>

<style scoped>
.sql-snippets {
  .snippets-container {
    height: 500px;
    display: flex;
    flex-direction: column;
  }

  .snippets-header {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .snippets-content {
    flex: 1;
    overflow: auto;
  }
}
</style> 